#!/bin/sh
# Generate initrd.uimg and update U-Boot boot.scr

# Kernel ABI string
ABI="${1}"

# List of paths to search for fdtfile in
FDT="\
/lib/firmware/${ABI}/device-tree/\${vendor} \
/lib/firmware/${ABI}/device-tree \
/usr/lib/linux-image-${ABI}/\${vendor} \
/usr/lib/linux-image-${ABI} \
/usr/lib/linux/\${vendor} \
/usr/lib/linux"

# Create U-Boot boot script
cat << EOF > /boot/boot.cmd
load \${devtype} \${devnum}:\${distro_bootpart} \${kernel_addr_r} /boot/vmlinuz-${ABI}
load \${devtype} \${devnum}:\${distro_bootpart} \${ramdisk_addr_r} /boot/initrd.img-${ABI}
setenv ramdisk_size \${filesize}
part uuid \${devtype} \${devnum}:\${distro_bootpart} partuuid
setenv bootargs root=PARTUUID=\${partuuid} rw rootwait
for fdtpath in ${FDT}; do
  if test -e \${devtype} \${devnum}:\${distro_bootpart} \${fdtpath}/\${fdtfile}; then
    load \${devtype} \${devnum}:\${distro_bootpart} \${fdt_addr_r} \${fdtpath}/\${fdtfile}
    bootz \${kernel_addr_r} \${ramdisk_addr_r}:\${ramdisk_size} \${fdt_addr_r}
  fi
done
bootz \${kernel_addr_r} \${ramdisk_addr_r}:\${ramdisk_size} \${fdtcontroladdr}
EOF

# Create U-Boot boot script
mkimage -A arm -T script -C none -d /boot/boot.cmd /boot/boot.scr
